/*
 * @Author: 0x9DEFA478
 * @Date: 2025-08-17 18:03:46
 * @LastEditTime: 2025-09-15 23:14:29
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * 
 */
#include "HMakeTargetTask_Command.hpp"
#include "SystemUtility/C_VirtualTerminal_Macro.h"




namespace HMake{

  TargetTask_Command::TargetTask_Command(const TargetConfig::Pointer& targetConfig
    ,const DependencyChain::Item::Pointer& hmkFileDependency
    ,const std::string& cwd,const std::string& shell
    ,DependencyItem_Directory::Collection::Pointer& DirectoryCollection
    ,Log::Pointer& log
    ,AttributeMask Attribute)
    :TargetTask(targetConfig,shell,Attribute)
    ,hmkFileDependency(hmkFileDependency)
    ,DirectoryCollection(DirectoryCollection)
    ,log(log)
    ,cwd(cwd)
    ,command(ReadCommand(targetConfig,"HMake.Shell.Command",shell))
    ,io_encoding(DependencyItem_SystemProcess::Task_Process::IOEncoding::Local)
  {
    //Command任务不需要PreBuild
    DisablePreBuild();

    std::string shellSuffix=shell;
    if(shellSuffix.length()>0){
      shellSuffix="."+shellSuffix;
    }

    const Value* vp=targetConfig->FindValue("HMake.Encoding.Shell"+shellSuffix);
    if((vp!=nullptr)&&(vp->Valid())&&(vp->ValueArray().Length()>0)){
      try{
        io_encoding=DependencyItem_SystemProcess::Task_Process::StringToIOEncoding(vp->ValueArray()[0]);
      }catch(const std::exception& e){
        throw std::runtime_error("$(HMake.Encoding.Shell"+shellSuffix+")无效: "+e.what());
      }
    }

  }

  TargetTask_Command::~TargetTask_Command(){}

  DependencyChain::Item::Pointer TargetTask_Command::MakeTask(){

    auto OutputFile=ReadValue_WithDefault(targetConfig,"HMake.Shell.OutputFilePath");

    std::list<DependencyItem_SystemProcess::Task::Pointer> commandTaskList;
    std::list<std::list<std::string>> argsList;

    for(auto& Value:command){
      std::list<std::string> args;

      for(auto& Item:Value.ValueArray()){
        args.emplace_back(Item);
      }

      argsList.emplace_back(args);
      commandTaskList.emplace_back(DependencyItem_SystemProcess::Task_Process::New(args,io_encoding));
    }

    std::string skipLog;
    std::string runLog;
    SetLogTag(
      runLog,"\e[" C_VT_F_Blue "mCommand\e[0m "+targetConfig->Name(),
      skipLog,"SKIP [Command "+targetConfig->Name()+"]",
      argsList
    );

    if(OutputFile.length()<1){
      auto r=DependencyItem_Command::New(commandTaskList,runLog,skipLog,log);
      r->SetIOExclusive(true);
      r->UpdateTargetStatus();
      return r;
    }

    auto r=DependencyItem_SystemProcess::New(OutputFile,commandTaskList,runLog,skipLog,log);
    r->SetIOExclusive(true);

    {
      auto InputFile=ReadValue_WithDefault(targetConfig,"HMake.Shell.InputFilePath");

      if(InputFile.length()>0){
        auto InputFileDep=DependencyItem_Input::New(InputFile);
        InputFileDep->UpdateTargetStatus();

        DependencyChain::AddDependency(r,InputFileDep);
      }
    }

    {
      auto OutputFileDirectory=(H::Path(OutputFile)+"../").DiscardRelative();
      auto OutputDirectoryCreateTask=DirectoryCollection->Insert(OutputFileDirectory,"\e[" C_VT_F_Green "mCreate Directory\e[0m: "+OutputFileDirectory.String(),"SKIP [Create Directory "+OutputFileDirectory.String()+"]",log);
      OutputDirectoryCreateTask->UpdateTargetStatus();
  
      DependencyChain::AddDependency(r,OutputDirectoryCreateTask);
    }

    r->UpdateTargetStatus();
    return r;
  }

}



